package com.itboye.ihomebank.activity.finance;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.bean.ZhuJiaBaoShouYeBean;
import com.itboye.ihomebank.constants.NetPublicConstant;
import com.itboye.ihomebank.custom.CarouselView;
import com.itboye.ihomebank.presenter.JinRongPresenter;
import com.itboye.ihomebank.util.DoubleUtil;
import com.itboye.ihomebank.util.LoweImageView;
import com.itboye.ihomebank.util.MyTimerFormat;
import com.itboye.ihomebank.util.StringFormatUtil;
import com.itboye.ihomebank.util.TimesUtils;
import com.itboye.ihomebank.util.XImageLoader;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 住家金融 首页
 */
public class FinanceActivity extends BaseOtherActivity implements Observer {
    View v_statusbar;
    ImageView close_icon;
    TextView add_shap_title_tv;// 标题
    Button btn_shouquantouzi, btn_woyaotouzi;
    CarouselView lunbo;
    TextView leiji_money, leiji_shouyi, txt_valuepercent;

    JinRongPresenter presenter;
    ZhuJiaBaoShouYeBean bean;

    Timer timer;
    private long start_time;
    private long end_time;
    private long time;//当前时间

    public static String DAOQI = "goumai.exit";// 该商品到期

    @Override
    protected int layout_id() {
        return R.layout.activity_finance;
    }

    @Override
    protected void onInitController() {

    }

    @Override
    protected void onInit() {
        setStatusBarHeight(v_statusbar);
        add_shap_title_tv.setText("住家月付");
        presenter = new JinRongPresenter(this);
        showProgressDialog("加载数据,请稍后...", true);
        presenter.zjbShouYe("5");

        timer = new Timer();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(timer!=null){
            timer.cancel();
        }
    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.close_icon:
                finish();
                break;
            case R.id.btn_woyaotouzi:
                intent = new Intent(this, TouZiActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_shouquantouzi:

                intent = new Intent(this, RentActivity.class);
                intent.putExtra("id", bean.getFinancial().get(0).getId());
                startActivity(intent);
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);

        if (entity.getEventType() == JinRongPresenter.zjb_shouye_success) {
            bean = (ZhuJiaBaoShouYeBean) entity.getData();

            read(bean);
        }
        if (entity.getEventType() == JinRongPresenter.zjb_shouye_fail) {

        }
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
    }

    /**
     * 加入数据
     * @param bean
     */
    private void read(ZhuJiaBaoShouYeBean bean) {
        String trade = DoubleUtil.doubleTransform(bean.getTotal_trade());
        String wholeStr = trade + "元";
        StringFormatUtil spanStr = new StringFormatUtil(this, wholeStr, trade, R.color.orange_yelow).fillColor();

        String profit = DoubleUtil.doubleTransform(bean.getTotal_profit());
        String wholeStr02 = profit + "元";
        StringFormatUtil spanStr02 = new StringFormatUtil(this, wholeStr02, profit, R.color.orange_yelow).fillColor();
        leiji_money.setText(spanStr.getResult());
        leiji_shouyi.setText(spanStr02.getResult());

        List<ZhuJiaBaoShouYeBean.Banner> bannerBean = new ArrayList<ZhuJiaBaoShouYeBean.Banner>();
        bannerBean.addAll(bean.getBanner());
        ArrayList<ImageView> arrayBmps = new ArrayList<>();
        for (int i = 0; i < bannerBean.size(); i++) {
            LoweImageView imageView = new LoweImageView(FinanceActivity.this);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            XImageLoader.load(NetPublicConstant.IMAGE_URL
                    + bannerBean.get(i).getImg(), imageView);
            arrayBmps.add(imageView);
            imageView.setTag(i);
        }
        lunbo.setImageBitmaps(arrayBmps);

        List<ZhuJiaBaoShouYeBean.Financial> financial = new ArrayList<ZhuJiaBaoShouYeBean.Financial>();
        financial.addAll(bean.getFinancial());
        txt_valuepercent.setText(DoubleUtil.doubleTransform(financial.get(0).getExpected_annualize()) + "%");

        time = Long.parseLong(TimesUtils.getStamp());
        start_time = Long.parseLong(bean.getFinancial().get(0).getStart_time());//开始时间
          start_time=1498034130;
        end_time = Long.parseLong(bean.getFinancial().get(0).getEnd_time());//结束时间
          end_time=1498034158;
        if (time > end_time) {
            btn_shouquantouzi.setText("授权投资(已过期)");
            btn_shouquantouzi.setEnabled(false);
            btn_shouquantouzi.setBackgroundResource(R.drawable.shape_send02);
        }
        if (time < start_time) {
            btn_shouquantouzi.setText("授权投资(还没开始)");
            btn_shouquantouzi.setEnabled(false);
            btn_shouquantouzi.setBackgroundResource(R.drawable.shape_send02);
        }
        timeCount();

    }

    private long onTime = 0;
    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            (FinanceActivity.this).runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    onTime++;
                    long time_dang = time + onTime;//当前时间

                    if (time_dang < start_time) {
                        btn_shouquantouzi.setText("授权投资(马上开始):   " + MyTimerFormat.ChangeToTime(start_time - time_dang));
                        btn_shouquantouzi.setEnabled(false);
                        btn_shouquantouzi.setBackgroundResource(R.drawable.shape_send02);

                        System.out.println("马上开始01："+onTime);
                    }
                    if (time_dang >= start_time && time_dang < end_time) {
                        btn_shouquantouzi.setText("授权投资(进行中)");
                        btn_shouquantouzi.setEnabled(true);
                        btn_shouquantouzi.setBackgroundResource(R.drawable.shape_send);
                        System.out.println("马上开始02："+onTime);
                    }
                    if (time_dang >= end_time) {
                        btn_shouquantouzi.setText("授权投资(已过期)");
                        btn_shouquantouzi.setEnabled(false);
                        btn_shouquantouzi.setBackgroundResource(R.drawable.shape_send02);
                        System.out.println("马上开始03："+onTime);
                        System.gc();
                        Intent intent=new Intent(FinanceActivity.DAOQI);
                        sendBroadcast(intent);//如果到期了发送广播结束掉已经打开的页面
                    }
                }
            });
        }
    };

    private void timeCount() {
        try {
            timer.schedule(task, 0, 1000);
        } catch (Exception e) {
        }
    }
}
